package d3_dodizhu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Room {
    private List<Card> allCards = new ArrayList<>();

    public Room() {
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] colors={"red", "green", "blue", "yellow"};
        int size =0;
        for (String number : numbers) {
            for (String color : colors) {//遍历花色
                size++;
                Card c = new Card(number,color,size);
                allCards.add(c);
            }
        }
        allCards.add(new Card("Jack","Queen",size++));
        allCards.add(new Card("Queen","King",size++));
        System.out.println("新牌"+allCards);
    }

    public void start() {
        Collections.shuffle(allCards);//洗牌
        System.out.println("洗牌"+allCards);

        //集合代表玩家
        List<Card> p1 = new ArrayList<>();
        List<Card> p2 = new ArrayList<>();
        List<Card> p3 = new ArrayList<>();

        for (int i =0;i<allCards.size()-3;i++) {//发牌
            Card c = allCards.get(i);
            if(i%3==0){
                p1.add(c);
            }
            else if(i%3==1){
                p2.add(c);
            }
            else if(i%3==2){
                p3.add(c);
            }
        }

        sortCards(p1);
        sortCards(p2);
        sortCards(p3);


        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
        List<Card> lastCards = allCards.subList(51,54);//包装最后3张(allCards.size()-3,allCards.size())
        p2.addAll(lastCards);
        sortCards(p2);

    }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()-o2.getSize();
            }
        });
    }
}
